package jwebtest.BPC;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.List;

import org.htmlparser.NodeFilter;
import org.htmlparser.Parser;
import org.htmlparser.filters.HasAttributeFilter;
import org.htmlparser.tags.TableColumn;
import org.htmlparser.tags.TableRow;
import org.htmlparser.tags.TableTag;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;

import com.alibaba.fastjson.JSON;

public class BPC2 {
	public static void main(String[] args) {
		new BPC2().doAction();
	}

	private String cookie = "wzwsconfirm=ebad8eea98a34576a00db8b946540c54; wzwstemplate=OQ==; ccpassport=3f0216fc3a9adc4575b1534e6dfccfb6; wzwschallenge=-1";

	public void doAction() {
		try {
			FileReader fr = new FileReader("E:\\支付机构.txt");
			BufferedReader br = new BufferedReader(fr);
			StringBuffer sb = new StringBuffer();
			String s;
			while ((s = br.readLine()) != null)
				sb.append(s);
			List<BpcPay> listPay = JSON.parseArray(sb.toString(), BpcPay.class);
			String host = "http://www.pbc.gov.cn";
			List<BpcPay> listAll=new ArrayList<BpcPay>();
			int count=0;
			for (BpcPay bp : listPay) {
				++count;
				if(count<260)
					continue;
				if(count%10==0){
					FileWriter fw = new FileWriter("E:\\支付机构-info"+count+".txt");
					fw.write(JSON.toJSONString(listAll));
					fw.close();
				}
				System.err.println(JSON.toJSON(bp));
				String html = BpcUtil.sendPost(host + bp.getLink_url(), cookie);
				BpcUtil.log(html);
				Parser parser = new Parser();
				parser.setInputHTML(html);
				parser.setEncoding("UTF-8");
				NodeList nodeList=getElement(parser, "class", "hei14jj").elementAt(0).getChildren();
				List<String[]> listStr=new ArrayList<String[]>();
				for (int i = 0; i < nodeList.size(); ++i) {
					int x=0;
					System.err.print("【" + i + "】");
					if (nodeList.elementAt(i) instanceof TableTag) {
						String[] sts=new String[18];
						TableTag tag = (TableTag) nodeList.elementAt(i);
						TableRow[] rows = tag.getRows();
						for (int j = 0; j < rows.length; ++j) {
							TableRow row = (TableRow) rows[j];
							TableColumn[] columns = row.getColumns();
							for (int k = 0; k < columns.length; ++k) {
								String str=columns[k].toPlainTextString().trim().replaceAll("&nbsp;", "");
								sts[x]=str;
								BpcUtil.log(str);
								++x;
							}// end for k
						}// end for j
						listStr.add(sts);
					}
				}
				List<BpcInfo> infos=new ArrayList<BpcInfo>();
				for (String[] ss : listStr) {
					infos.add(new BpcInfo(ss[0], ss[2], ss[4], ss[6], ss[8],ss[10].split(","), ss[12], ss[14], ss[16]));
				}
				BpcPay bp2=JSON.parseObject(JSON.toJSONString(bp), BpcPay.class);
				bp2.setInfos(infos);
				listAll.add(bp2);
				Thread.currentThread().sleep(1500);
			}
			FileWriter fw = new FileWriter("E:\\支付机构-info-X-"+count+".txt");
			fw.write(JSON.toJSONString(listAll));
			fw.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public NodeList getElement(Parser parser, String tag, String name) throws ParserException {
		parser.reset();// 重置
		NodeFilter filter = new HasAttributeFilter(tag, name);
		return parser.extractAllNodesThatMatch(filter);
	}
}
